TeeChartPHP
[ class tree: TeeChartPHP ] [ index: TeeChartPHP ] [ all elements ]

Source for file Smith.php

Documentation is available at Smith.php

  1. <?php
  2.  
  3. /**
  4.  * Smith class
  5.  *
  6.  * Description: Smith Series
  7.  *
  8.  * @author
  9.  * @copyright (c) 1995-2010 by Steema Software SL. All Rights Reserved. <info@steema.com>
  10.  * @version 1.0
  11.  * @package TeeChartPHP
  12.  * @subpackage styles
  13.  * @link http://www.steema.com
  14.  */
  15.  
  16.  class Smith extends Circular {
  17.  
  18.     private $circlePen;
  19.     private $pen;
  20.     private $imagSymbol;
  21.     private $pointer;
  22.     private $oldX;
  23.     private $oldY;
  24.  
  25.     public function Smith($c=null) {
  26.         parent::Circular($c);
  27.  
  28.         $this->getXValues()->name = Language::getString("SmithResistance");
  29.         $this->getXValues()->setOrder(ValueListOrder::$NONE);
  30.         $this->getYValues()->name = Language::getString("SmithReactance");
  31.         $this->pointer = new SeriesPointer($this->chart, $this);
  32.         $this->circlePen = new ChartPen($c,Color::BLACK());
  33.         $this->imagSymbol = "i";
  34.         //$this->setCircleBackColor(Color::getEmpty());
  35.     }
  36.  
  37.     /**
  38.       * Defines pen to draw SmithSeries Chart.
  39.       *
  40.       * @return ChartPen 
  41.       */
  42.     public function getPen() {
  43.         if ($this->pen == null) {
  44.             $this->pen = new ChartPen($this->chart);
  45.         }
  46.         return $this->pen;
  47.     }
  48.  
  49.     /**
  50.       * Defines pen to draw SmithSeries Chart.
  51.       *
  52.       * @return SeriesPointer 
  53.       */
  54.     public function getPointer() {
  55.         if ($this->pointer == null) {
  56.             $this->pointer = new SeriesPointer($this->chart, $this);
  57.         }
  58.         return $this->pointer;
  59.     }
  60.  
  61.     /**
  62.       * Specifies the text used to display together with axis labels around a
  63.       * Smith circle series.<br>
  64.       * Default value: ImagSymbol is a lowercase "i" letter.
  65.       *
  66.       * @return String 
  67.       */
  68.     public function getImagSymbol() {
  69.         return $this->imagSymbol;
  70.     }
  71.  
  72.     /**
  73.       * Specifies the text used to display together with axis labels around a
  74.       * Smith circle series.<br>
  75.       * Default value: ImagSymbol is a lowercase "i" letter.
  76.       *
  77.  
  78.       * @param value String
  79.       */
  80.     public function setImagSymbol($value) {
  81.         $this->imagSymbol = $this->setStringProperty($this->imagSymbol, $value);
  82.     }
  83.  
  84.     /**
  85.       * Defines Brush to fill Chart.
  86.       *
  87.       * @return ChartBrush 
  88.       */
  89.     public function getBrush() {
  90.         return $this->bBrush;
  91.     }
  92.  
  93.     protected function addSampleValues($numValues) {
  94.         $r = $this->randomBounds($numValues);
  95.         for ( $t = 0; $t <= $numValues; $t++) {
  96.             $this->add(6.5 * $t / $numValues, ($t * $r->Random() + 3.8) / $numValues);
  97.         }
  98.     }
  99.  
  100.     /**
  101.       * Defines Pen to draw CCircles of the Smith Series.<br>
  102.       * xCircle (reactance circle) pen. <br>
  103.       * Read CCirclePen to obtain the TChartPen object that represents the color
  104.       * and style of the CCircles of the Smith Series.<br>
  105.       * Use CCirclePen to change the color or style.
  106.       *
  107.       * @return GridPen 
  108.       */
  109.     public function getCCirclePen() {
  110.         return $this->getVertAxis()->getGrid();
  111.     }
  112.  
  113.     /**
  114.       * Defines Pen to draw RCircles of the Smith Series.<br>
  115.       * rCircle (resistance circle) pen. <br>
  116.       * Read RCirclePen to obtain the Pen object that represents the color and
  117.       * style of the RCircles of the Smith Series.<br>
  118.       * Use RCirclePen to change the color or style.
  119.       *
  120.       * @return GridPen 
  121.       */
  122.     public function getRCirclePen() {
  123.         return $this->getHorizAxis()->getGrid();
  124.     }
  125.  
  126.     /**
  127.       * Defines Pen to draw external Circle of the Smith Series.<br>
  128.       * Resistance = 0 rCircle pen (biggest circle). <br>
  129.       * Read CirclePen to obtain the Pen object that represents the color and
  130.       * style of the external circle of the Smith Series.<br>
  131.       * Use CirclePen to change the color or style.
  132.       *
  133.       * @return ChartPen 
  134.       */
  135.     public function getCirclePen() {
  136.         if ($this->circlePen == null) {
  137.             $this->circlePen = new ChartPen($this->chart);
  138.         }
  139.         return $this->circlePen;
  140.     }
  141.  
  142.     private $DEFAULTX =  Array (0, 0.1, 0.3, 0.5, 0.8, 1, 1.5, 2, 3, 5, 7, 10);
  143.     
  144.     /**
  145.        $Shows/hides $the $static $finalant $reactance $labels.<br>
  146.       * When set to true, constant reactance labels are shown.
  147.       *
  148.       * @return boolean 
  149.       */
  150.     public function getCLabels() {
  151.         return $this->getVertAxis()->getLabels()->getVisible();
  152.     }
  153.  
  154.     /**
  155.        $Shows/hides $the $static $finalant $reactance $labels.<br>
  156.       * When set to true, constant reactance labels are shown.
  157.       *
  158.       * @param value boolean
  159.       */
  160.     public function setCLabels($value) {
  161.         $this->getVertAxis()->getLabels()->setVisible($value);
  162.     }
  163.  
  164.     /**
  165.        $Shows/hides $the $static $finalant $resistance $labels.<br>
  166.       * When set to true constant resistance labels are shown.
  167.       *
  168.       * @return boolean 
  169.       */
  170.     public function getRLabels() {
  171.         return $this->getHorizAxis()->getLabels()->getVisible();
  172.     }
  173.  
  174.     /**
  175.        $Shows/hides $the $static $finalant $resistance $labels.<br>
  176.       * When set to true constant resistance labels are shown.
  177.       *
  178.       * @param value boolean
  179.       */
  180.     public function setRLabels($value) {
  181.         $this->getHorizAxis()->getLabels()->setVisible($value);
  182.     }
  183.  
  184.     private function drawXCircleGrid() {
  185.         $this->chart->getGraphics3D()->setPen($this->getCCirclePen());
  186.         for ( $t = 0; $t <= 11; $t++) {
  187.             $this->drawXCircle($this->DEFAULTX[$t], $this->getMiddleZ(), $this->getCLabels());
  188.         }
  189.     }
  190.  
  191.     private $DEFAULTR = Array(0, 0.2, 0.5, 1, 2, 5, 10);
  192.  
  193.     private function drawRCircleGrid() {
  194.         $this->chart->getGraphics3D()->setPen($this->getRCirclePen());
  195.         for ( $t = 0; $t <= 6; $t++) {
  196.             $this->drawRCircle($this->DEFAULTR[$t], $this->getMiddleZ(), $this->getRLabels());
  197.         }
  198.     }
  199.  
  200.     private function drawAxis() {
  201.         if ($this->getVertAxis()->getVisible()) {
  202.             $this->drawXCircleGrid();
  203.         }
  204.  
  205.         $this->chart->getGraphics3D()->unClip();
  206.  
  207.         if ($this->getHorizAxis()->getVisible()) {
  208.             $this->drawRCircleGrid();
  209.         }
  210.     }
  211.  
  212.     private function drawCircle() {
  213.         $g = $this->chart->getGraphics3D();
  214.  
  215.         //CDI CircleGradient
  216.         if ($this->getCircleBackColor()->isEmpty() && $this->calcCircleGradient() == null)         /* 5.02 */
  217.         {
  218.             $g->getBrush()->setVisible(false);
  219.         } 
  220.         else 
  221.         {
  222.             $g->getBrush()->setVisible(true);
  223.             $g->getBrush()->setSolid(true);
  224.             $g->getBrush()->setColor($this->calcCircleBackColor());
  225.             $g->getBrush()->setGradient($this->getCircleGradient());
  226.         }
  227.         
  228.         $tmpX = $this->getCircleWidth() / 2;
  229.         $tmpY = $this->getCircleHeight() / 2;
  230.         $g->setPen($this->getCirclePen());
  231.         $g->ellipse($this->getCircleXCenter() - $tmpX, $this->getCircleYCenter() - $tmpY,
  232.                   $this->getCircleXCenter() + $tmpX, $this->getCircleYCenter() + $tmpY, $this->getEndZ());
  233.     }
  234.  
  235.     protected function doBeforeDrawValues() {
  236.         $first = false;
  237.  
  238.         for ( $t = 0; $t < $this->chart->getSeriesCount(); $t++) {
  239.              $s = $this->chart->getSeries($t);
  240.             if ($s->getActive() && ($s instanceof Smith)) {
  241.                 if ($s === $this) {
  242.                     if (!$first) {
  243.                         if ($this->getCLabels()) {
  244.                             $r = $this->chart->getChartRect();
  245.                             $r->grow( -$this->chart->getGraphics3D()->textWidth("360"),
  246.                                    -$this->chart->getGraphics3D()->getFontHeight() + 2);
  247.                             $this->chart->setChartRect($r);
  248.                         }
  249.                     }
  250.                     break;
  251.                 }
  252.  
  253.                 $first = true;
  254.             }
  255.         }
  256.  
  257.        parent::doBeforeDrawValues();
  258.  
  259.        $first = false;
  260.  
  261.        for ( $tt = 0; $tt < $this->chart->getSeriesCount(); $tt++) {
  262.            $ss = $this->chart->getSeries($tt);
  263.            if ($ss->getActive() && ($ss instanceof Smith)) {
  264.                if ($ss === $this) {
  265.                    if (!$first) {
  266.                        $this->drawCircle();
  267.                        if ($this->chart->getAxes()->getVisible() &&
  268.                            $this->chart->getAxes()->getDrawBehind()) {
  269.                            $this->drawAxis();
  270.                        }
  271.                    }
  272.                    break;
  273.                }
  274.            $first = true;
  275.           }
  276.       }
  277.     }
  278.  
  279.     protected function draw() {
  280.  
  281.         parent::draw();
  282.  
  283.         if ($this->pointer->getVisible()) {
  284.  
  285.             for ( $t = $this->firstVisible; $t <= $this->lastVisible; $t++) {
  286.                 $tmpColor = $this->getValueColor($t);
  287.                 $this->pointer->prepareCanvas($this->chart->getGraphics3D(), $tmpColor);
  288.                 $this->pointer->draw($this->calcXPos($t), $this->calcYPos($t), $tmpColor);
  289.             }
  290.         }
  291.     }
  292.  
  293.         /**
  294.           * Called internally. Draws the "ValueIndex" point of the Series.
  295.           *
  296.           * @param valueIndex int
  297.           */
  298.     public function drawValue($valueIndex) {
  299.         $p = $this->zToPos($this->vxValues->getValue($valueIndex),
  300.                          $this->vyValues->getValue($valueIndex));
  301.         $this->linePrepareCanvas($valueIndex);
  302.         if ($valueIndex == $this->firstVisible) {
  303.             $this->chart->getGraphics3D()->moveTo($p, $this->getStartZ()); // <-- first point
  304.         } else
  305.         if (($p->x != $this->oldX) || ($p->y != $this->oldY)) {
  306.             $this->chart->getGraphics3D()->lineTo($p, $this->getStartZ());
  307.         }
  308.  
  309.         $this->oldX = $p->x;
  310.         $this->oldY = $p->y;
  311.     }
  312.  
  313.     private function getXCircleLabel($reactance) {
  314.         return $reactance . $this->imagSymbol;
  315.     }
  316.  
  317.     private function linePrepareCanvas($valueIndex) {
  318.         $g = $this->chart->getGraphics3D();
  319.         if ($this->getPen()->getVisible()) {
  320.             $g->setPen($this->getPen());
  321.             $g->getPen()->setColor(($valueIndex == -1) ? $this->getColor() :
  322.                                 $this->getValueColor($valueIndex));
  323.         } else {
  324.             $g->getPen()->setVisible(false);
  325.         }
  326.     }
  327.  
  328.     public function prepareForGallery($isEnabled) {
  329.         parent::prepareForGallery($isEnabled);
  330.         $this->chart->getAspect()->setChart3DPercent(5);
  331.         $this->chart->getAxes()->getRight()->getLabels()->setVisible(false);
  332.         $this->chart->getAxes()->getTop()->getLabels()->setVisible(false);
  333.         $this->chart->getAspect()->setOrthogonal(false);
  334.         $this->chart->getAspect()->setElevation(360);
  335.         $this->chart->getAspect()->setZoom(90);
  336.     }
  337.  
  338.     public function setChart($c) {
  339.         parent::setChart($c);
  340.         if ($this->pointer != null) {
  341.             $this->pointer->setChart($c);
  342.         }
  343.         if (($this->chart != null) && $this->Beans->isDesignTime()) {
  344.             $this->chart->getAspect()->setView3D(false);
  345.         }
  346.     }
  347.  
  348.         /**
  349.           * The pixel Screen Horizontal coordinate of the ValueIndex Series
  350.           * value.<br>
  351.           * This coordinate is calculated using the Series associated Horizontal Axis.
  352.           *
  353.           * @param valueIndex int
  354.           * @return int 
  355.           */
  356.     public function calcXPos($valueIndex) {
  357.         return $this->zToPos($this->vxValues->getValue($valueIndex),
  358.                       $this->vyValues->getValue($valueIndex))->x;
  359.     }
  360.  
  361.         /**
  362.           * The pixel Screen Vertical coordinate of the ValueIndex Series
  363.           * value.<br>
  364.           * This coordinate is calculated using the Series associated Vertical Axis.
  365.           *
  366.           * @param valueIndex int
  367.           * @return int 
  368.           */
  369.     public function calcYPos($valueIndex) {
  370.         return $this->zToPos($this->vxValues->getValue($valueIndex),
  371.                       $this->vyValues->getValue($valueIndex))->y;
  372.     }
  373.  
  374.         /**
  375.           * The ValueIndex of the "clicked" point in the Series.<br>
  376.           *
  377.           * @param x int
  378.           * @param y int
  379.           * @return int 
  380.           */
  381.     public function clicked($x, $y) {
  382.         if ($this->chart != null) {
  383.             $p = $this->chart->getGraphics3D()->calculate2DPosition($x, $y, $this->getStartZ());
  384.             $x = $p->x;
  385.             $y = $p->y;
  386.         }
  387.  
  388.         $result = parent::clicked($x, $y);
  389.  
  390.         if (($result == -1) && ($this->firstVisible > -1) && ($this->lastVisible > -1)) {
  391.             if ($this->pointer->getVisible()) {
  392.                 for ( $t = $this->firstVisible; $t <= $this->lastVisible; $t++) {
  393.                     if (($this->abs($this->calcXPos($t) - $x) < $this->pointer->getHorizSize()) &&
  394.                         ($this->abs($this->calcYPos($t) - $y) < $this->pointer->getVertSize())) {
  395.                         return $t;
  396.                     }
  397.                 }
  398.             }
  399.         }
  400.  
  401.         return $result;
  402.     }
  403.  
  404.     private function drawRCircleLabel($rVal, $x, $y) {
  405.         if ($this->getHorizAxis()->getVisible()) {
  406.             /* 5.02 */
  407.             $this->chart->getGraphics3D()->setFont($this->getRLabelsFont());
  408.             $tmpStringAlignment = new StringAlignment();
  409.             $this->chart->getGraphics3D()->setTextAlign($tmpStringAlignment->CENTER);
  410.             $this->chart->getGraphics3D()->textOut($x, $y, $this->getEndZ(), $rVal);
  411.         }
  412.     }
  413.  
  414.     private function drawRCircle($value, $z, $showLabel) {
  415.         if ($value != -1) {
  416.              // Transform R
  417.              $tmp = 1 / (1 + $value);
  418.              $halfXSize = MathUtils::round($tmp * $this->getXRadius());
  419.              $halfYSize = MathUtils::round($tmp * $this->getYRadius());
  420.  
  421.             // Circles are always right aligned
  422.             $this->chart->getGraphics3D()->ellipse($this->getCircleRect()->getRight() -
  423.                                           2 * $halfXSize,
  424.                                           $this->getCircleYCenter() - $halfYSize,
  425.                                           $this->getCircleRect()->getRight(),
  426.                                           $this->getCircleYCenter() + $halfYSize, $z);
  427.                                           
  428.  
  429.             if ($showLabel) { // 5.02 ( if $RLabels)
  430.                 $this->drawRCircleLabel($value,
  431.                                  $this->getCircleRect()->getRight() - 2 * $halfXSize,
  432.                                  $this->getCircleYCenter());
  433.             }
  434.         }
  435.     }
  436.  
  437.         /**
  438.           * xCircle labels font.
  439.           *
  440.           * @return ChartFont 
  441.           */
  442.     public function getCLabelsFont() {
  443.         return $this->getVertAxis()->getLabels()->getFont();
  444.     }
  445.  
  446.         /**
  447.           * rCircle labels font.
  448.           *
  449.           * @return ChartFont 
  450.           */
  451.     public function getRLabelsFont() {
  452.         return $this->getHorizAxis()->getLabels()->getFont();
  453.     }
  454.  
  455.     private function drawXCircleLabel($xVal, $x, $y) {
  456.  
  457.         if ($this->getVertAxis()->getVisible()) {
  458.             /* 5.02 */
  459.             $this->chart->getGraphics3D()->setFont($this->getCLabelsFont());
  460.             $tmpHeight = $this->chart->getGraphics3D()->getFontHeight();
  461.             $tmpSt = $this->getXCircleLabel($xVal);
  462.  
  463.             $angle = $this->pointToAngle($x, $y) * 57.29577;
  464.  
  465.             if ($angle >= 360) {
  466.                 $angle -= 360;
  467.             }
  468.             if (($angle == 0) || ($angle == 180)) {
  469.                 $y -= $tmpHeight / 2;
  470.             } else
  471.             if (($angle > 0) && ($angle < 180)) {
  472.                 $y -= $tmpHeight;
  473.             }
  474.             if (($angle == 90) || ($angle == 270)) {
  475.                 $tmpStringAlignment = new StringAlignment();
  476.                 $this->chart->getGraphics3D()->setTextAlign($tmpStringAlignment->CENTER);
  477.             } else {
  478.                 $this->chart->getGraphics3D()->setTextAlign((($angle > 90) &&
  479.                         ($this->angle < 270)) ?
  480.                         $tmpStringAlignment->FAR :
  481.                         $tmpStringAlignment->NEAR);
  482.             }
  483.  
  484.              $tmpWidth = $this->chart->getGraphics3D()->textWidth("0") / 2;
  485.  
  486.             if ($angle == 0) {
  487.                 $x += $tmpWidth;
  488.             } else if ($angle == 180) {
  489.                 $x -= $tmpWidth;
  490.             }
  491.  
  492.             $this->chart->getGraphics3D()->textOut($x, $y, $this->getEndZ(), $tmpSt);
  493.         }
  494.     }
  495.  
  496.     private function drawXCircle($value, $z, $showLabel) {
  497.  
  498.         $p1 = new TeePoint();
  499.         $p2 = new TeePoint();
  500.  
  501.         if ($value != 0) {
  502.  
  503.             $invValue = 1 / $value;
  504.             $p4 = $this->zToPos(0, $value); // Endpos
  505.             if ($showLabel) {
  506.                 $this->drawXCircleLabel($value, $p4->x, $p4->y);
  507.             }
  508.  
  509.             $p3 = $this->zToPos(100, $value); // Startpos
  510.  
  511.             // ellipse bounding points
  512.             $halfXSize = MathUtils::round($invValue * $this->getXRadius());
  513.             $halfYSize = MathUtils::round($invValue * $this->getYRadius());
  514.  
  515.             $p1->x = $this->getCircleRect()->getRight() - $halfXSize;
  516.             $p2->x = $this->getCircleRect()->getRight() + $halfXSize;
  517.             $p1->y = $this->getCircleYCenter();
  518.             $p2->y = $p1->y - 2 * $halfYSize;
  519.  
  520.             // TODO $this->chart->getGraphics3D()->clipEllipse($this->getCircleRect());
  521.             if ((!$this->chart->getAspect()->getView3D()) ||
  522.                 $this->chart->getAspect()->getOrthogonal()) {
  523.                 $this->chart->getGraphics3D()->arc($p1->x, $p2->y, $p2->x, $p1->y, 0, 360);
  524.             }
  525.  
  526.             $p4 = $this->zToPos(0, -$value); // Endpos
  527.  
  528.             if ((!$this->chart->getAspect()->getView3D()) ||
  529.                 $this->chart->getAspect()->getOrthogonal()) {
  530.                 $this->chart->getGraphics3D()->arc($p1->x, $p2->y + 2 * $halfYSize, $p2->x,
  531.                                           $p1->y + 2 * $halfYSize, 0, 360);
  532.             }
  533.             // TODO $this->chart->getGraphics3D()->unClip();
  534.  
  535.             if ($showLabel) {
  536.                 $this->drawXCircleLabel( -$value, $p4->x, $p4->y);
  537.             }
  538.         } else {
  539.             /* special case ) reactance is zero */
  540.             $p1->x = $this->getCircleRect()->x;
  541.             $p2->x = $this->getCircleRect()->getRight();
  542.             $p1->y = $this->getCircleYCenter();
  543.  
  544.             $this->chart->getGraphics3D()->line($p1->x, $p1->y, $p2->x, $p1->y, $this->getMiddleZ());
  545.  
  546.             if ($showLabel) {
  547.                 $this->drawXCircleLabel(0, $p1->x, $p1->y);
  548.             }
  549.         }
  550.     }
  551.  
  552.     // Position to impendance
  553.     // (ZRe,ZIm)=(1+gamma)/(1-gamma)
  554.     private function posToZ($x, $y) {
  555.         $x -= $this->getCircleXCenter();
  556.         $y = $this->getCircleYCenter() - $y;
  557.         $gRe = $x / $this->getXRadius();
  558.         $gIm = $y / $this->getYRadius();
  559.         $norm2 = ($gRe * $gRe) + ($gIm * $gIm);
  560.         $invDen = 1 / ($norm2 - 2 * $gRe + 1);
  561.         return new PointDouble((1 - $norm2) * $invDen, 2 * $gIm * $invDen);
  562.     }
  563.  
  564.     // impendance to Position
  565.     // (GRe,GIm)=(1-z)/(1+z)
  566.     private function zToPos($resist, $react) {
  567.  
  568.         $norm2 = ($resist * $resist) + ($react * $react);
  569.         $invDen = 1 / ($norm2 + 2 * $resist + 1);
  570.         $gRe = ($norm2 - 1) * $invDen;
  571.         $gIm = 2 * $react * $invDen;
  572.         return new TeePoint($this->getCircleXCenter() + MathUtils::round($gRe * $this->getXRadius()),
  573.                          $this->getCircleYCenter() - MathUtils::round($gIm * $this->getYRadius()));
  574.     }
  575.  
  576.     /**
  577.       * Gets descriptive text.
  578.       *
  579.       * @return String 
  580.       */
  581.     public function getDescription() {
  582.         return Language::getString("GallerySmith");
  583.     }
  584. }
  585.  
  586. ?>

Documentation generated on Wed, 16 Jun 2010 11:25:32 +0200 by phpDocumentor 1.4.1